﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class CorporationController : EntityController<Corporation, string, CorporationSearch, ICorporationViewModel, ICorporationAppService>, ICorporationController
    {
        private IIndustryTypeAppService IndustryTypeAppService { get; set; }
        private IAreaAppService AreaAppService { get; set; }

        public override string ControllerName
        {
            get { return ControllerConst.CORPORATION; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public CorporationController()
        {
            this.Model = ModelFactory.CreateModel<ICorporationViewModel>();
        }


        [ActionName(ActionConst.INDEX)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "corporation_query")]
        public ActionResult Index()
        {
            this.Model.Item = this.AppService.GetById(this.Model.MasterModel.CurrentUserModel.CorporationCode);
           
            IList<IndustryType> industries = this.IndustryTypeAppService.GetBySearch(SearchFactory.CreateIndustryTypeSearch()).ToList();
            foreach (IndustryType item in industries)
            {
                SelectListItem li = new SelectListItem();
                li.Text = item.ChinesName;
                li.Value = item.IDToDisplay;
                li.Selected = (this.Model.Item.IsNotNull() && this.Model.Item.Industry.IsNotNull() && this.Model.Item.Industry.IDToDisplay == li.Value);
                this.Model.IndustryList.Add(li);
            }
            this.AreaAppService.InitAreaList();
            //this.Model.IndustryList.Add(new SelectListItem() { Text = "添加...", Value = "-1" });
            this.Model.CityID = this.Model.Item.Area.Id;
            this.Model.ProvinceID = Area.Cities.Where(r => r.Id == this.Model.Item.Area.Id).Select(r => r.ParentCode).FirstOrDefault();
            return View("~/Views/System/v1/addCorporation.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "corporation_edit")]
        public JsonResult Save(CorporationViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.Area = this.AreaAppService.GetById(model.Item.Area.Id);
            model.Item.Industry = this.IndustryTypeAppService.GetById(model.Item.Industry.Id);
            BoolGenericResult<Corporation> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);
            return Json(jsonResult, "application/json");
        }

    }
}
